
#include "semantic_analyzer.h"
#include "semantics_namespace.h"
#include "semantics_enum.h"
#include "semantics_field.h"
#include "semantics_method.h"
#include "semantics_model.h"
#include "semantics_property.h"
#include "generic_table.cpp"

#include "main_container.h"
#include "errors_warnings.h"
extern MainContainer * CompilerData;

SemanticsNamespace::SemanticsNamespace()
{
	this->Name = new GenericTable<int>();
	this->CurrentContext = -1;

	this->Classes = new GenericTable<SemanticsClass>();
	this->Enums = new GenericTable<SemanticsEnum>();
	this->Fields = new GenericTable<SemanticsField>();
	this->Methods = new GenericTable<SemanticsMethod>();
	this->Models = new GenericTable<SemanticsModel>();
	this->Namespaces = new GenericTable<SemanticsNamespace>();
	this->Properties = new GenericTable<SemanticsProperty>();
	this->Simulations = new GenericTable<SemanticsSimulation>();
}



SemanticsNamespace::~SemanticsNamespace()
{
	if(Name != NULL)
	{
		delete Name;
		Name = NULL;
	}

	if(Classes != NULL)
	{
		delete Classes;
		Classes = NULL;
	}

	if(Enums != NULL)
	{
		delete Enums;
		Enums = NULL;
	}

	if(Fields != NULL)
	{
		delete Fields;
		Fields = NULL;
	}

	if(Methods != NULL)
	{
		delete Methods;
		Methods = NULL;
	}

	if(Models != NULL)
	{
		delete Models;
		Models = NULL;
	}

	if(Namespaces != NULL)
	{
		delete Namespaces;
		Namespaces = NULL;
	}

	if(Properties != NULL)
	{
		delete Properties;
		Properties = NULL;
	}

	if(Simulations != NULL)
	{
		delete Simulations;
		Simulations = NULL;
	}
}



#ifdef DEBUG
void SemanticsNamespace::DebugXMLOutput(FILE * fo)
{
	// starting tag
	fprintf(fo, "<namespace current_context=\"%d\" name=\"", this->CurrentContext);

	// name inside starting tag
	SemanticAnalyzer::DebugOutputNameFromTable(fo, this->Name, CompilerData->Units);

	fprintf(fo, "\">\n");


	// Output subnamespaces.
	fprintf(fo, "<namespaces>\n");
	for(int i = 0; i < this->Namespaces->GetCount(); i++)
	{
		SemanticsNamespace * sn = this->Namespaces->GetData(i);

		sn->DebugXMLOutput(fo);
	}

	fprintf(fo, "</namespaces>\n");

	
	// Output classes.
	fprintf(fo, "<classes>\n");
	for(int i = 0; i < this->Classes->GetCount(); i++)
	{
		SemanticsClass * sc = this->Classes->GetData(i);

		sc->DebugXMLOutput(fo);
	}

	fprintf(fo, "</classes>\n");


	// Output fields.
	fprintf(fo, "<fields>\n");
	for(int i = 0; i < this->Fields->GetCount(); i++)
	{
		SemanticsField * sf = this->Fields->GetData(i);

		sf->DebugXMLOutput(fo);
	}

	fprintf(fo, "</fields>\n");


	// Output properties.
	fprintf(fo, "<properties>\n");
	for(int i = 0; i < this->Properties->GetCount(); i++)
	{
		SemanticsProperty * sp = this->Properties->GetData(i);

		sp->DebugXMLOutput(fo);
	}

	fprintf(fo, "</properties>\n");


	// Output enums.
	fprintf(fo, "<enums>\n");
	for(int i = 0; i < this->Enums->GetCount(); i++)
	{
		SemanticsEnum * se = this->Enums->GetData(i);

		se->DebugXMLOutput(fo);
	}

	fprintf(fo, "</enums>\n");



	// Output methods.
	fprintf(fo, "<methods>\n");
	for(int i = 0; i < this->Methods->GetCount(); i++)
	{
		SemanticsMethod * sm = this->Methods->GetData(i);

		sm->DebugXMLOutput(fo);
	}

	fprintf(fo, "</methods>\n");


	// Output models.
	fprintf(fo, "<models>\n");
	for(int i = 0; i < this->Models->GetCount(); i++)
	{
		SemanticsModel * sm = this->Models->GetData(i);

		sm->DebugXMLOutput(fo);
	}

	fprintf(fo, "</models>\n");


	// Output simulations.
	fprintf(fo, "<simulations>\n");
	for(int i = 0; i < this->Simulations->GetCount(); i++)
	{
		SemanticsSimulation * sim = this->Simulations->GetData(i);

		sim->DebugXMLOutput(fo);
	}

	fprintf(fo, "</simulations>\n");


	// end tag
	fprintf(fo, "</namespace>\n");

}
#endif
